//
//  MineCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/20.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "MineCell.h"

@implementation MineCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    self.leftLabel = [UILabel k_labelWithFontSize:13 textColor:UIColor.k_t2Color];
    self.descLabel = [UILabel k_labelWithFontSize:13 textColor:UIColor.k_t3Color];
    self.descLabel.numberOfLines = 0;
//    self.descLabel.textAlignment = NSTextAlignmentRight;
    self.rightImgView = [UIImageView new];
    [self.leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.leftLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
//    self.leftLabel.highlightedTextColor =
//    self.descLabel.highlightedTextColor =
    
    [self.pContentView addSubview:self.leftLabel];
    [self.pContentView addSubview:self.descLabel];
    [self.pContentView addSubview:self.rightImgView];
    
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.bottom.mas_offset(-15).priorityLow();
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightImgView.mas_left).offset(-10);
//        make.centerY.mas_offset(0);
        make.top.equalTo(self.leftLabel);
//        make.left.equalTo(self.leftLabel.mas_right).offset(10);
        make.bottom.mas_lessThanOrEqualTo(-15);
    }];

    [self.rightImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
        make.size.mas_equalTo(CGSizeMake(12, 16));
    }];
//
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
    self.selectedBackgroundView.backgroundColor = UIColor.whiteColor;
    self.backgroundColor =
    self.contentView.backgroundColor = global_viewBackgroundColor;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(YBContentModel *)model {
    self.leftLabel.text = model.title;
    self.descLabel.text = model.rightText;

    CGFloat descMaxWidth = ScreenWidth - (15 + self.leftLabel.intrinsicContentSize.width + 10 + 15);
    
    if (model.icon.isHaveStr) {
        self.rightImgView.image = UIImage.k_imgName(model.icon);
        [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightImgView.mas_left).offset(-10);
        }];
        descMaxWidth -= 10;
        if ([model.icon isEqualToString:@"arrow_right"] == YES) {
            [self.rightImgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(6, 11));
            }];
            descMaxWidth -= 11;
        } else {
            [self.rightImgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(12, 16));
            }];
            descMaxWidth -= 16;
        }
    } else {
        self.rightImgView.image = nil;
        [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightImgView.mas_left).offset(0);
        }];
        [self.rightImgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(0, 0));
        }];
    }
    
    self.descLabel.preferredMaxLayoutWidth = descMaxWidth;
}

@end
